<?php
/**
 * Created by Kevin.
 * User: Administrator
 * Date: 2017/6/15
 * Time: 16:58
 */

namespace common\service;
use Yii;

class SmsService extends BaseService
{
    const URL = "http://www.stongnet.com/sdkhttp/sendsms.aspx";

    //发验证接口
    public static function send($phone, $str)
    {
        $msg = rawurlencode("【XXX珠宝交易平台】验证码为:".$str."，用于XXX珠宝交易平台相关注册服务验证，10分钟内有效，如有疑问请联系客服。");
        $strSmsParam = "reg=".Yii::$app->params['key']."&pwd=".Yii::$app->params['pwd']."&sourceadd=&phone={$phone}&content=" . $msg;
        $result = self::postSend($strSmsParam);
        if (empty($result))
        {
            return array('status' => -1, 'msg' => '短信发送失败');
        }
        parse_str($result, $explode);
        return array('status' => $explode['result'], 'msg' => $explode['message']);
    }

    /**
     * 发送账号密码
     */

    public static function sendaccount($phone,$name,$account,$password)
    {
        
        $str = "【XXX珠宝交易平台】尊敬的".$name."先生/女士：你的XXX珠宝结算系统登录账户为：".$account."，密码：".$password."，登录网址：www.xxx.com，请立即登录修改您的密码及完善个人信息，如有疑问请联系客服。";
        $msg = rawurlencode($str);
        $strSmsParam = "reg=".Yii::$app->params['key']."&pwd=".Yii::$app->params['pwd']."&sourceadd=&phone={$phone}&content=" . $msg;
        $result = self::postSend($strSmsParam);
        if (empty($result))
        {
            return array('status' => -1, 'msg' => '短信发送失败');
        }
        parse_str($result, $explode);
        return array('status' => $explode['result'], 'msg' => $explode['message']);
    }

    public static function postSend($param)
    {
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,self::URL);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$param);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

}